package com.woniuxy.light_smart.service.imp;

import com.woniuxy.light_smart.controller.LoginCardController;
import com.woniuxy.light_smart.dto.ApplyDto;
import com.woniuxy.light_smart.dto.LoginDto;
import com.woniuxy.light_smart.dto.UpdatePwdDto;
import com.woniuxy.light_smart.dto.UserDto;
import com.woniuxy.light_smart.entity.User;
import com.woniuxy.light_smart.entity.UserRole;
import com.woniuxy.light_smart.mapper.RoleMapper;
import com.woniuxy.light_smart.mapper.UserMapper;
import com.woniuxy.light_smart.mapper.UserRoleMapper;
import com.woniuxy.light_smart.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import com.woniuxy.light_smart.vo.UserVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    HttpSession session;
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserRoleMapper userRoleMapper;
    @Override
    public User selectByTelAndPsw(LoginDto loginDto) {

        LoginCardController loginCardController = new LoginCardController();
        String s = loginCardController.map.get(loginDto.getUuid());
                if(!loginDto.getCard().equals(s)){
            return null;
        }
        return userMapper.selectByTelAndPsw(loginDto);
    }

    @Override

    public List<User> selectAll() {
        return userMapper.selectAll();
    }
    @Override
    public int applyUser(ApplyDto applyDto) {
        User user = userMapper.selectByTelOrMail(applyDto.getTel());
        User user2 = userMapper.selectByMail(applyDto.getMail());
        if(user2==null&&user==null&&applyDto.getPwd().equals(applyDto.getPwd2())){
            User user1 = new User();
            user1.setUserTel(applyDto.getTel());
            user1.setUserName(applyDto.getName());
            user1.setUserMail(applyDto.getMail());
            user1.setUserPwd(applyDto.getPwd());
            user1.setUserSex(applyDto.getSex());
            user1.setAreaId(applyDto.getAreaId());
            user1.setUserImg(applyDto.getImg());
            userMapper.insertSelective(user1);
            User user3 = userMapper.selectByTelOrMail(applyDto.getTel());
            UserRole userRole = new UserRole();
            userRole.setUserId(user3.getUserId());
            userRole.setRoleId(1);
            userRoleMapper.insertSelective(userRole);
            return 1;
        }
        return 2;
    }

    @Override
    public User selectByTelOrMail(String tel) {
        return userMapper.selectByTelOrMail(tel);
    }

    @Override

    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updatePwd(UpdatePwdDto updatePwdDto) {
        return userMapper.updatePwd(updatePwdDto);
    }
    public List<UserVo> selectAllUserByLike(UserDto userDto) {
        return userMapper.selectAllUserByLike(userDto);
    }

    @Override
    public void insertToUser(UserDto userDto) {
        userMapper.insertUser(userDto);

    }

    @Override
    public UserVo selectByUid(Long id) {
        return userMapper.selectByUid(id);
    }

    @Override
    @Transactional
    public int deleteByPrimaryKey(Long id) {
        userRoleMapper.deleteRidByUid(id);
         userMapper.deleteByPrimaryKey(id);
         return 11111;
    }

    @Override
    public int updateUser(UserDto userDto) {
        return userMapper.updateUser(userDto);
    }


}
